package com.vanke.kotlin

import android.content.Context
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

/**
 * @name Kotlin
 * @package name：com.vanke.kotlin
 * @class describe
 * @anthor shiyuchuan
 * @time 2017/7/24 11:47
 */
object DelegateExt{
    fun <T> notNullSingleValueVar(): ReadWriteProperty<Any?,T> = NotNullSingleValueVar()
    fun <T : Any> prefrence(context: Context, name: String, default: T)
    = Prefrence(context = context, name = name, default = default)
}

class NotNullSingleValueVar<T>() : ReadWriteProperty<Any?,T>{

    private var value: T? = null
    override fun getValue(thisRef: Any?, property: KProperty<*>): T {
        return value ?: throw IllegalStateException()
    }

    override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
        this.value = if (this.value==null) value
        else throw IllegalStateException()
    }

}